﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Common;
using Module;
using BLL;
using EMPower.Utility;

namespace App
{
    public partial class User_UserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void btn_submitUserInfo_ServerClick(object sender, EventArgs e)
        {
            AppAuthHelper.SessionCheck();
            if (ViewState["UserProfileInfo"] != null)
            {
                UserProfileInfo userInfo = ViewState["UserProfileInfo"] as UserProfileInfo;
                if (userInfo == null || userInfo.UserPassportId == 0)
                {
                    Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "页面失效，刷新页面后请重试", Contexts.ThisPageUrlWithParams);
                }

                userInfo.NickName = Request["txt_nickName"];
                userInfo.Adress = Request["txt_adress"];
                if (!string.IsNullOrEmpty(Request["txt_birthday"]))
                {
                    userInfo.Birthday = DateTime.Parse(Request["txt_birthday"].Trim());
                }

                userInfo.Email = Request["txt_email"];
                userInfo.IDCardNum = Request["txt_IdNum"];
                userInfo.Mobile = Request["txt_PhoneNumber"];
                userInfo.RealName = Request["txt_realName"];
                //this.img_Photo.Src = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Display) + ;
                if (this.file != null && !string.IsNullOrEmpty(this.file.FileName))
                {

                    //上传图片

                    int filelenth = file.PostedFile.ContentLength;
                    int fileSize = Convert.ToInt32((filelenth) / 1024);
                    if (fileSize > 5000)
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('图片大小不超过5M');</script>");
                        return;
                    }
                    string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."));
                    string strFileName = IdHelper.GetLongId().ToString() + "_" + DateTime.Now.Millisecond.ToString() + fileType;
                    string strNewFileName = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Upload) + strFileName;
                    file.SaveAs(strNewFileName);
                    userInfo.IDCardImg = strFileName;
                }
                if (Request["DDL_provinceRequest"] != "-1")
                {
                    userInfo.ProvinceId = int.Parse(Request["DDL_province"]);
                }
                if (Request["DDL_city"] != "-1")
                {
                    userInfo.CityId = int.Parse(Request["DDL_city"]);
                }
                userInfo.LastUpdate = DateTime.Now;
                BUserProfile.getInstance().Update(userInfo);
                pageInit();
            }
            else
            {
                Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "页面失效，刷新页面后请重试", Contexts.ThisPageUrlWithParams);
            }
        }

        protected void btn_submitPassword_ServerClick(object sender, EventArgs e)
        {
            AppAuthHelper.SessionCheck();
            string oldpassword = txt_oldpassword.Text.Trim();
            string newpassword = txt_newPassword.Text.Trim();
            string surepassword = txt_surepassword.Text.Trim();
            UserPassportInfo eu = BUserPassport.getInstance().GetUserPassportInfo(AppAuthHelper.CurrentUserPassportId);
            if (eu != null)
            {
                if (CheckPassword(oldpassword, newpassword, surepassword))
                {

                    eu.Password = txt_newPassword.Text.Trim();
                    BUserPassport.getInstance().Update(eu);

                    Community.Utility.JavaScriptHelper.MessageBox(this, "修改成功！");//修改成功！

                    AppAuthHelper.CurrentUserInfo.Password = eu.Password;



                }
                else
                {
                    Community.Utility.JavaScriptHelper.MessageBox(this, "修改失败！");//修改失败！
                    //SetDiv(1, 1, 2);
                    lbl_userpassword.Visible = true;
                }
            }
        }

        private bool CheckPassword(string oldpassword, string newpassword, string surepassword)
        {
            bool isok = true;
            // 空判断
            if (string.IsNullOrEmpty(oldpassword) || string.IsNullOrEmpty(newpassword) || string.IsNullOrEmpty(surepassword))
            {
                lbl_userpassword.Text = "输入项不可以为空!";//输入项不可以为空!
                isok = false;
            }
            else if (AppAuthHelper.CurrentUserInfo.Password != oldpassword)
            {
                lbl_userpassword.Text = "原密码不正确!"; //原密码不正确!
                isok = false;
            }
            else if (newpassword != surepassword)
            {
                lbl_userpassword.Text = "确认密码与新密码不一致!"; //确认密码与新密码不一致!
                isok = false;
            }
            return isok;
        }

        private void pageInit()
        {

            List<AreaInfo> areas = BArea.getInstance().GetAreaByParentId(0);

            if (areas != null && areas.Count > 0)
            {
                this.DDL_province.DataSource = areas;
                this.DDL_province.DataTextField = "AreaName";
                this.DDL_province.DataValueField = "AreaCode";
                this.DDL_province.DataBind();
            }

            long userId = Contexts.GetUserPassportId();
            if (userId > 0)
            {
                UserProfileInfo userInfo = BUserProfile.getInstance().GetUserProfileInfo(userId);
                if (userInfo != null && userInfo.UserPassportId > 0)
                {
                    ViewState["UserProfileInfo"] = userInfo;
                    this.txt_nickName.Text = userInfo.NickName;
                    this.txt_adress.Text = userInfo.Adress;
                    if (!userInfo.Birthday.Equals(DateTime.MinValue))
                    {
                        this.txt_birthday.Text = userInfo.Birthday.ToString("yyyy-MM-dd");
                    }
                    this.txt_email.Text = userInfo.Email;
                    this.txt_IdNum.Text = userInfo.IDCardNum;
                    this.txt_PhoneNumber.Text = userInfo.Mobile;
                    this.txt_realName.Text = userInfo.RealName;
                    this.img_Photo.Src = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Display) + userInfo.IDCardImg;
                    if (userInfo.ProvinceId > 0)
                    {
                        this.DDL_province.SelectedValue = userInfo.ProvinceId.ToString();
                        areas = BArea.getInstance().GetAreaByParentId(userInfo.ProvinceId);
                        this.DDL_city.DataSource = areas;
                        this.DDL_city.DataTextField = "AreaName";
                        this.DDL_city.DataValueField = "AreaCode";
                        this.DDL_city.DataBind();
                        if (userInfo.CityId > 0)
                        {
                            this.DDL_city.SelectedValue = userInfo.CityId.ToString();
                        }
                    }
                    else
                    {
                        this.DDL_province.SelectedValue = "-1";
                    }
                }
                if (AppAuthHelper.CurrentUserPassportId > 0 && userId == AppAuthHelper.CurrentUserPassportId)//本人进入页面可修改
                {
                    this.divUserInfo.Disabled = true;
                    this.divJS.InnerHtml = "<script>myDataHelper(\"txt_birthday\", \"1980-1-1\", \"yy-mm-dd\");</script>";
                    this.txt_nickName.ReadOnly = false;
                    this.txt_adress.ReadOnly = false;
                    //this.txt_birthday.Attributes.Remove("onkeypress");
                    this.txt_birthday.ReadOnly = false;
                    this.txt_email.ReadOnly = false;
                    this.txt_IdNum.ReadOnly = false;
                    this.txt_PhoneNumber.ReadOnly = false;
                    this.txt_realName.ReadOnly = false;

                    this.DDL_province.Enabled = true;
                    this.DDL_city.Enabled = true;
                }
                else
                {
                    //this.form1.Disabled = true;

                    this.liUploadImg.Visible = false;
                    this.liIdCardNum.Visible = false;
                    this.liImgLable.Visible = false;

                    this.txt_nickName.ReadOnly = true;
                    this.txt_adress.ReadOnly = true;
                    //this.txt_birthday.Attributes.Remove("onkeypress");
                    this.txt_birthday.ReadOnly = true;
                    this.txt_email.ReadOnly = true;
                    this.txt_IdNum.ReadOnly = true;
                    this.txt_PhoneNumber.ReadOnly = true;
                    this.txt_realName.ReadOnly = true;

                    this.DDL_province.Enabled = false;
                    this.DDL_city.Enabled = false;
                    this.btn_submitUserInfo.Visible = false;

                    this.divChangePW.Visible = false;
                }
            }
            else
            {
                int userType = Contexts.GetUserType();
                Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "用户不存在，请重新选择用户", Contexts.AppPath + "/Index.aspx?" + Contexts.UserType + "=" + Contexts.GetEncrypt(userType.ToString()));
            }
        }
    }
}